<?php

namespace app\admin\model\wdsxh\mall;

use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'wdsxh_mall_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'buy_now_text',
        'status_text',
        'refund_status_text',
        'paid_text',
        'pay_time_text',
        'refund_time_text',
        'complete_time_text',
        'cancel_time_text'
    ];
    

    
    public function getBuyNowList()
    {
        return ['1' => __('Buy_now 1'), '2' => __('Buy_now 2')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'),'4' => __('Status 4')];
    }

    public function getRefundStatusList()
    {
        return ['1' => __('Refund_status 1'), '2' => __('Refund_status 2'), '3' => __('Refund_status 3'), '4' => __('Refund_status 4')];
    }
    public function getPaidList()
    {
        return ['0' => __('Paid 0'), '1' => __('Paid 1')];
    }


    public function getBuyNowTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['buy_now']) ? $data['buy_now'] : '');
        $list = $this->getBuyNowList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRefundStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_status']) ? $data['refund_status'] : '');
        $list = $this->getRefundStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaidTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paid']) ? $data['paid'] : '');
        $list = $this->getPaidList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getRefundTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['refund_time']) ? $data['refund_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCompleteTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['complete_time']) ? $data['complete_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCancelTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cancel_time']) ? $data['cancel_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setRefundTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCompleteTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCancelTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function mallorderrefund()
    {
        return $this->belongsTo('app\admin\model\wdsxh\mall\Refund', 'id', 'order_id', [], 'LEFT')->setEagerlyType(0);
    }

    public function goods(){
     return   $this->hasMany('app\admin\model\wdsxh\mall\Item','order_id','id')->field('order_id,goods_num,goods_name,goods_price,goods_image,unit,pay_price');
    }



}
